<?php
class update_db extends CI_Model{

    public function __construct()
    {
        // Call the Model constructor
        $this->load->database();
    }

    function check_id_avail($id)
    {
        $query = $this->db->query("SELECT COUNT(ID) as num
                                   FROM sanpham
                                   WHERE ID = $id");
        $data = $query->row();
        if($data->num == '0')
            return 0;
        else
            return 1;
        
    }    
    
    function get_product_branch($id)
    {
        $query = $this->db->query("SELECT TenLoai
                                   FROM loai
                                   WHERE id IN (SELECT idLoai
                                                FROM sanpham
                                                WHERE ID = $id)");
        $data = $query->row();
        return $data->TenLoai;
    }
    
    function get_product_type($id)
    {
        $query = $this->db->query("SELECT idLoai
                                   FROM sanpham
                                   WHERE ID = $id");
        $data = $query->row();
        if($data == NULL)
            return false;
        else
            return $data->idLoai;
    }
    
    function get_product_name($id)
    {
        $query = $this->db->query("SELECT TenSP
                                   FROM sanpham
                                   WHERE ID = $id");
        $data = $query->row();
        if($data == NULL)
            return false;
        else
            return $data->TenSP;
    }
    
    function update_product_name($id,$name)
    {  
       $query = $this->db->query("UPDATE sanpham
                                   SET sanpham.TenSP = '$name'
                                   WHERE ID = $id");                        
       if($query)
            return true;
       else
            return false;
    }
    
    function get_product_price($id)
    {
        $query = $this->db->query("SELECT Gia
                                   FROM sanpham,gia
                                   WHERE sanpham.ID = gia.idSP
                                   AND ID = $id");
        $data = $query->row();
        if($data == NULL)
            return false;
        else
            return $data->Gia;
    }
    
    function update_product_price($id,$price)
    {
        $query = $this->db->query("UPDATE gia
                                   SET gia.Gia = '$price'
                                   WHERE idSP = $id");                        
       if($query)
            return true;
       else
            return false;
    }
    
    function get_product_info($id,$id_info_product_type)
    {
        
        $query = $this->db->query('SELECT ThongSo
                                   FROM thongsosp
                                   WHERE idSP = '.$id.
                                 ' AND idTS = '.$id_info_product_type);
        $data = $query->row();
        if($data == NULL)
            return false;
        else
            return $data->ThongSo;
    }
    
    function update_product_info($id,$id_info_product_type,$update_info)
    {
        $query = $this->db->query("UPDATE thongsosp
                                   SET ThongSo = '$update_info'
                                   WHERE idSP = '$id'
                                   AND idTS = '$id_info_product_type'");
        if($query)
            return true;
        else
            return false;
    }
    
    function get_product_img_path($id)
    {
        $query = $this->db->query("SELECT hinhanh.DuongDan
                                   FROM sanpham,hinhanh,anhlon
                                   WHERE sanpham.ID = anhlon.idSP
                                   AND anhlon.idAnh = hinhanh.id
                                   AND sanpham.ID = $id ");
        
        return $query->result();
    }
    
    function update_product_img_path($id,$img_data,$description)
    {
        $id_pic = date("hismd");
        var_dump($id_pic);
        $check = $this->db->query("SELECT COUNT(DuongDan) AS num
                                    FROM hinhanh
                                    WHERE id = '$id_pic'");
        $checkdata = $check->row();
        var_dump($checkdata);
        if($checkdata->num != 0)
        {
            $query = $this->db->query("UPDATE hinhanh
                                       SET id = '".$id_pic."',
                                           DuongDan = './static/".$img_data['upload_data']['file_name']."',
                                           MoTa = '$description'");
            $query = $this->db->query("UPDATE anhlon
                                       SET idAnh = '".$id_pic."',
                                           Rong = '".$img_data['upload_data']['image_width']."',
                                           Cao = '".$img_data['upload_data']['image_height']."'
                                        WHERE idSP = '$id'");
        }
        else
        {
            $query = $this->db->query("INSERT INTO hinhanh (id,DuongDan,MoTa)
                                   VALUE ('".$id_pic."','./static/".$img_data['upload_data']['file_name']."','".$description."')"); 
            $query = $this->db->query("INSERT INTO anhlon (idSP,idAnh,Rong,Cao)
                                   VALUE ('".$id."','".$id_pic."','".$img_data['upload_data']['image_width']."','".$img_data['upload_data']['image_height']."')"); 

        }
    }
    
    function update_product_thumb_path($id,$thumb_path,$description)
    {
        $id_pic = date("mdhis");
        $check = $this->db->query("SELECT COUNT(DuongDan) AS num
                                    FROM hinhanh
                                    WHERE id = '$id_pic'");
        $checkdata = $check->row();
        var_dump($checkdata);
        if($checkdata->num != 0)
        {
            $query = $this->db->query("UPDATE hinhanh
                                        SET id = '".$id_pic."',
                                            DuongDan = './static/".$thumb_path."',
                                            MoTa = '$description'");
            $query = $this->db->query("UPDATE thumbnail
                                       SET idAnh = '".$id_pic."'
                                       WHERE idSP = '$id'");
        }
        else
        {
             $query = $this->db->query("INSERT INTO hinhanh (id,DuongDan,MoTa)
                                   VALUE ('".$id_pic."','".$thumb_path."','".$description."')"); 
             $query = $this->db->query("INSERT INTO thumbnail (idSP,idAnh)
                                   VALUE ('".$id."','".$id_pic."')"); 
        }
    }
    
    function get_product_img_description($id)
    {
        $query = $this->db->query("SELECT hinhanh.MoTa
                                   FROM sanpham,hinhanh,anhlon
                                   WHERE sanpham.ID = anhlon.idSP
                                   AND anhlon.idAnh = hinhanh.id
                                   AND sanpham.ID = $id ");
        $data = $query->row();
        if($data == NULL)
            return false;
        else
            return $data->MoTa;
    }
 }
 ?>